#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/17  19:13
# @Author: 余浪人
# @email: yulangren520@gmail.com
import re
from flask import request
from wtforms import Form, StringField, TextAreaField, BooleanField, validators, RadioField, ValidationError,DateTimeField


class TL_System_Form(Form):
    web_name = StringField(label='站点名称',
                           render_kw={"class": "layui-input", "placeholder": "请输入站点名称", "style": "width:30%;"})
    web_subtitle = StringField(label='站点副标题',
                               render_kw={"class": "layui-input", "placeholder": "请输入站点名称", "style": "width:50%;"})
    web_domain = StringField(label='前台域名',validators=[validators.URL(message='域名格式有误!'),validators.DataRequired('请填写域名!')],
                             render_kw={"class": "layui-input", "placeholder": "http://", "style": "width:40%;"})
    web_logo = StringField(label='网站LOGO',
                           render_kw={"class": "layui-input", "style": "width:40%;display: inline-block;",
                                      "readonly": "readonly"})
    web_ico = StringField(label='浏览器ICO',
                          render_kw={"class": "layui-input", "style": "width:40%;display: inline-block;",
                                     "readonly": "readonly"})
    web_key = StringField(label='站点关键词',
                          render_kw={"class": "layui-input", "placeholder": "请输入关键词", "style": "width:70%;"})
    web_description = TextAreaField(label='站点描述', render_kw={"placeholder": "请输入描述内容", "class": "layui-textarea",
                                                             "style": "width:60%;"})
    web_stat = TextAreaField(label='统计代码',
                             render_kw={"placeholder": "请输入统计代码", "class": "layui-textarea", "style": "width:60%;"})
    web_close_tips = TextAreaField(label='站点提示', render_kw={"placeholder": "请输入关闭站点提示", "class": "layui-textarea",
                                                            "style": "width:60%;"})
    web_company = StringField(label='公司名称',
                              render_kw={"class": "layui-input", "placeholder": "请输入公司名称", "style": "width:40%;"})
    web_contact_adders = StringField(label='联系地址',
                                     render_kw={"class": "layui-input", "placeholder": "请输入联系地址",
                                                "style": "width:40%;"})
    web_contact_tel = StringField(label='联系电话',
                                  validators=[validators.Optional(True), validators.Regexp('1[3578]\d{9}', message='联系电话格式有误!'), validators.Length(max=11, message='联系电话格式有误!')],
                                  render_kw={"class": "layui-input", "placeholder": "请输入联系电话", "style": "width:40%;"})
    web_info_copyright = StringField(label='版权信息',
                                     render_kw={"class": "layui-input", "placeholder": "请输入版权信息",
                                                "style": "width:40%;"})
    web_record = StringField(label='ICP备案号',
                             render_kw={"class": "layui-input", "placeholder": "请输入备案号", "style": "width:30%;"})
    web_gongan = StringField(label='公安备案号',
                             render_kw={"class": "layui-input", "placeholder": "请输入公备号", "style": "width:30%;"})
    web_contact_qq = StringField(label='联系QQ',
                                 render_kw={"class": "layui-input", "placeholder": "请输入QQ", "style": "width:40%;"})
    web_contact_email = StringField(label='联系邮箱',
                                    validators=[validators.Optional(True), validators.Email('邮箱格式有误!')],
                                    render_kw={"class": "layui-input", "placeholder": "请输入邮箱地址", "lay-verify": "email", "style": "width:40%;"})
    web_content_on_off = BooleanField('系统评论', render_kw={"lay-skin": "switch", "lay-text": "开启|关闭"})
    web_on_off = BooleanField('站点开关', render_kw={"lay-skin": "switch", "lay-text": "开启|关闭"})
    web_type = RadioField('站点类型',coerce=int,choices=[(1,'动态'),(2,'静态')])


class TL_Notice_Form(Form):
    notice_title = StringField('公告标题', validators=[validators.DataRequired('标题不可为空!')],
                               render_kw={"class": "layui-input", "placeholder": "请输入公告标题", "style": "width:50%;"})
    notice_promulgator = StringField('发布者', validators=[validators.DataRequired('发布者不可为空!'),
                                                        validators.Length(max=10, message='发布者过长!')],
                                     render_kw={"class": "layui-input", "placeholder": "请输入公告标题",
                                                "style": "width:30%;"}, default='admin')
    notice_content = TextAreaField('公告内容', validators=[validators.DataRequired('请填写公告内容!')],
                                   render_kw={"class": "layui-textarea", "style": "width: 80%;"})


class TL_Ad_Form(Form):
    ad_name = StringField(label='广告名称', validators=[validators.DataRequired(message="广告名称不可为空")],
                          render_kw={"class": "layui-input", "placeholder": "请输入广告名称", "lay-verify": "title",
                                     "style": "width: 200px;"})
    ad_end_date = DateTimeField(label='到期时间', validators=[validators.DataRequired('请输入到期时间!')],
                            render_kw={"class": "layui-input", "lay-verify": "date", "style": "width: 200px;"})
    ad_code = TextAreaField(label='广告内容', render_kw={"class": "layui-textarea", "style": "width: 80%;",'placeholder':'请输入广告内容'})
    ad_remark = StringField(label='备注信息', render_kw={"class": "layui-input", "style": "width: 80%;",'placeholder':'输入广告位备注'})
    status = BooleanField(label='状态', render_kw={"lay-skin": "switch", "lay-text": "开启|关闭", "style": "width: 50px;"})


class TL_Blogroll_Form(Form):
    link_name = StringField(label='链接名称', validators=[validators.DataRequired(message='链接名不可为空!')],
                            render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 50%;",'placeholder':'请输入链接名称'})
    link_url = StringField(label='链接地址', default='http://',
                           validators=[validators.DataRequired('链接地址不可为空!'), validators.URL(message='地址格式有误!')],
                           render_kw={"class": "layui-input", "lay-verify": "url|required", "style": "width: 60%;",'placeholder':'请输入链接URl'})
    link_end_date = DateTimeField(label='到期时间', validators=[validators.DataRequired('请输入到期时间!')],
                              render_kw={"class": "layui-input", "style": "width: 200px;",'placeholder':'请选择到期时间'})
    link_color = StringField(label='链接颜色', validators=[validators.DataRequired(message='请选择颜色!')],
                             render_kw={"class": "layui-input", "style": "width: 20%;", "readonly": "readonly",'placeholder':'请选择链接颜色'})
    link_sort = StringField(label='排序',
                            validators=[validators.Optional(True), validators.Regexp(r'^\d*$', message='排序格式为整数!')],
                            default=0, render_kw={"class": "layui-input", "style": "width: 50px;"})
    link_remark = StringField(label='备注信息',
                              render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 60%;",'placeholder':'请输入备注信息'})
    status = BooleanField(label='状态', render_kw={"lay-skin": "switch", "lay-text": "开启|关闭", "style": "width: 50px;"})


class TL_Pay_Form(Form):
    app_id = StringField('APP_ID',
                         validators=[validators.DataRequired('请配置 APP_ID!'), validators.Regexp(r'^\d*$', message='APP_ID格式不正确!')], render_kw={"class": "layui-input",'placeholder':'请填写支付宝APPID'})
    public_key = TextAreaField('PUBLIC_KEY', validators=[validators.DataRequired('请配置 PUBLIC_KEY!')],
                               render_kw={"class": "layui-textarea", 'style': 'width: 85%;','placeholder':'请填入支付宝配置的公钥'})
    private_key = TextAreaField('PRIVATE_KEY', validators=[validators.DataRequired('请配置 PRIVATE_KEY!')],
                                render_kw={"class": "layui-textarea", 'style': 'width: 85%;','placeholder':'请填入支付宝配置的私钥'})


class TL_Redis_Form(Form):
    redis_host = StringField(label='Host', validators=[validators.IPAddress(message='Host格式为IP_v4 地址'),
                                                       validators.DataRequired('请填写Host地址!')],
                             render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 200px;"})
    redis_port = StringField(label='端口', validators=[validators.Regexp(r'^\d+$', message='端口为整数!')],
                             render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 100px",
                                        "oninput": "value=value.replace(/[^\d]/g,'')"})
    redis_password = StringField(label='密码', default='',
                                 render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 50%;",'placeholder':'请输入Redis密码,不填为空'})

    def validate_redis_port(self, redis_port):
        val = int(redis_port.data)
        if val > 65535 or val < 1:
            raise validators.ValidationError('端口值最大为:65535 最小为:1')


class TL_Email_Form(Form):
    MAIL_PROT = StringField(label='服务器端口', validators=[validators.Regexp(r'^\d+$', message='端口为整数!')],
                            render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 100px",
                                       "oninput": "value=value.replace(/[^\d]/g,'')"})
    MAIL_SERVER = StringField(label='邮件服务器', validators=[validators.DataRequired('请填写邮件服务器地址!')],
                              render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 300px;",'placeholder':'请填写邮件服务器地址'})

    MAIL_USERNAME = StringField(label='发送邮箱号', validators=[validators.DataRequired('请填写邮件发送账号!'), validators.Email('邮件发送账号格式不正确!')],
                                render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 300px",'placeholder':'请填入发信账号'
                                           })
    MAIL_PASSWORD = StringField(label='邮箱授权码', validators=[validators.DataRequired('请填写邮箱授权码!')],
                                render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 300px;",'placeholder':'请填写授权码'})
    MAIL_USE_SSL = BooleanField(label='开启SSL', render_kw={"lay-skin": "switch", "lay-text": "开启|关闭"})


class TL_Save_Form(Form):
    save_classification=RadioField(label='存储方式',validators=[validators.DataRequired('请选择存储方式')],coerce=int,choices=[(1,'本地存储'),(2,'TL_OSS存储'),(3,'七牛云存储')])
    save_path = StringField(label='存储目录', validators=[validators.DataRequired('存储目录不可为空'), validators.Length(2, 32, '存储目录配置过长')],render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 150px;",'placeholder':'uploads/'})
    bucket_name = StringField(label='空间名称', render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 300px;",'placeholder':'例如:tlcms'})
    bucket_domain = StringField(label='空间域名', render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 300px;",'placeholder':'例如:http://static.tlcms.net'})
    access_key = StringField(label='Access_Key',  render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 50%;",'placeholder':'请配置Access_Key'})
    secret_key = StringField(label='Secret_Key', render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 50%;",'placeholder':'请配置Secret_Key'})
    def validate_save_classification(self, save_classification):
        if save_classification.data==2 or save_classification.data==3:
            bucket_name=request.form.get('bucket_name') # TL_OSS存储
            if not len(bucket_name):
                raise ValidationError('请配置空间名称')
            elif len(bucket_name)<2 or len(bucket_name)>64:
                raise ValidationError('请配置正确的空间名称')
            bucket_domain=request.form.get('bucket_domain')
            regex = r'^[a-z]+://(?P<host>[^/:]+)(?P<port>:[0-9]+)?(?P<path>\/.*)?$'
            res = re.compile(regex)
            if not len(bucket_domain):
                raise ValidationError('请配置空间域名')
            elif len(bucket_domain) < 5 or len(bucket_domain) > 64:
                raise ValidationError('请配置正确的空间域名')
            elif not res.match(bucket_domain):
                raise ValidationError('空间域名格式有误')
            access_key=request.form.get('access_key')
            if not len(access_key):
                raise ValidationError('请配置Access_Key')
            elif len(access_key) < 30 or len(access_key)>64:
                raise ValidationError('请配置正确的Access_Key')
            secret_key=request.form.get('secret_key')
            if not len(secret_key):
                raise ValidationError('请配置Secret_Key')
            elif len(secret_key) < 30 or len(secret_key)>64:
                raise ValidationError('请配置正确的Secret_Key')
        elif save_classification.data!=1:
            raise ValidationError("存储方式非法!")